﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DalGen.Entity;
using WeifenLuo.WinFormsUI.Docking;
using DalGen.Enum;
using DalGen.Contracts;
using ICSharpCode.TextEditor.Document;
using DalGen.Service;

namespace DalGen
{
	public partial class TableField : DockContent
	{
		List<ETableInfo> tableInfo;
		IBuilder builder;
		private string highlightingStrategy = "C#";

		private BuilderType radioBtnAttr = BuilderType.Entity;

		private void TableField_Load(object sender, EventArgs e)
		{
			BindField();
			SetCommonAttr();
			SetCombox();
			this.rdoEntity.CheckedChanged += new EventHandler(radioBtn_CheckedChange);
			this.rdoIRepository.CheckedChanged += new EventHandler(radioBtn_CheckedChange);
			this.rdoIService.CheckedChanged += new EventHandler(radioBtn_CheckedChange);
			this.rdoMapping.CheckedChanged += new EventHandler(radioBtn_CheckedChange);
			this.rdoRepository.CheckedChanged += new EventHandler(radioBtn_CheckedChange);
			this.rdoService.CheckedChanged += new EventHandler(radioBtn_CheckedChange);
			this.rdoServiceModel.CheckedChanged += new EventHandler(radioBtn_CheckedChange);
		}

		private void SetCommonAttr()
		{
			txtTitle.Text = builder.ProjectInfo.Title;
			txtVersion.Text = builder.ProjectInfo.Version;
			txtCompany.Text = builder.ProjectInfo.Company;
			txtCopyRight.Text = builder.ProjectInfo.CopyRight;
			txtDesigner.Text = builder.ProjectInfo.Designer;
			txtEmail.Text = builder.ProjectInfo.Email;
			txtProjectNameSpace.Text = builder.ProjectInfo.ProjectNameSpace;
		}

		public TableField(List<ETableInfo> table, IBuilder ibuilder)
		{
			this.tableInfo = table;
			this.builder = ibuilder;
			InitializeComponent();
		}

		public void BindField()
		{
			foreach (var item in tableInfo)
			{
				ListViewItem lvitem = new ListViewItem();
				lvitem.Text = item.ColumnID.ToString();
				lvitem.ImageIndex = 0;

				lvitem.SubItems.Add(item.ColumnName);
				lvitem.SubItems.Add(item.SystemTypeName);
				lvitem.SubItems.Add(item.MaxLength.ToString());
				lvitem.SubItems.Add(item.Scale.ToString());
				lvitem.SubItems.Add(item.PrimaryKey == true ? "√" : "");
				lvitem.SubItems.Add(item.Identity == true ? "√" : "");
				lvitem.SubItems.Add(item.Nullable == true ? "√" : "");
				lvitem.SubItems.Add(item.Default.Replace("(", "").Replace(")", ""));
				lvitem.SubItems.Add(item.Description);
                lvitem.SubItems.Add(FluentValidation.GetRule(item));
				listView1.Items.Add(lvitem);
			}
		}

		private void btnselall_Click(object sender, EventArgs e)
		{

			for (int i = 0; i < listView1.Items.Count; i++)
			{
				listView1.Items[i].Checked = true;
			}
		}

		private void btnunselall_Click(object sender, EventArgs e)
		{
			for (int i = 0; i < listView1.Items.Count; i++)
			{
				listView1.Items[i].Checked = false;
			}
		}

		//生成
		private void btnGeneration_Click(object sender, EventArgs e)
		{
			builder.ProjectInfo.Columns = tableInfo;
			string code = builder.Building(radioBtnAttr);
			SetTextEditor(code);
			this.tabControl1.SelectedTab = this.tabPage2;
		}

		#region radioBtn事件
		public void radioBtn_CheckedChange(object sender, EventArgs e)
		{
			if (!((RadioButton)sender).Checked)
			{
				return;
			}

			switch (((RadioButton)sender).Text)
			{
				case "Entity":
					this.radioBtnAttr = BuilderType.Entity;
					highlightingStrategy = "C#";
					//SetGroupbox(grpEntity);
					break;
				case "IRepository":
					this.radioBtnAttr = BuilderType.IRepository;
					highlightingStrategy = "C#";
					//SetGroupbox(grpIRepository);
					break;
				case "IService":
					this.radioBtnAttr = BuilderType.IService;
					highlightingStrategy = "C#";
					//SetGroupbox(grpIService);
					break;
				case "Mapping":
					this.radioBtnAttr = BuilderType.Mapping;
					highlightingStrategy = "XML";
					//SetGroupbox(grpEntity);
					break;
				case "Repository":
					this.radioBtnAttr = BuilderType.Repository;
					highlightingStrategy = "C#";
					//SetGroupbox(grpRepository);
					break;
				case "Service":
					this.radioBtnAttr = BuilderType.Service;
					highlightingStrategy = "C#";
					//SetGroupbox(grpService);
					break;
				case "ServiceModel":
					this.radioBtnAttr = BuilderType.ServiceModel;
					highlightingStrategy = "C#";
					//SetGroupbox(grpServiceModel);
					break;
				default:
					break;
			}
		}
		#endregion

		private void SetGroupbox(GroupBox box)
		{
			//grpEntity.Visible = false;
			//grpIRepository.Visible = false;
			//grpIService.Visible = false;
			//grpRepository.Visible = false;
			//grpService.Visible = false;
			//grpServiceModel.Visible = false;
			box.Visible = true;

		}

		private void SetTextEditor(string value)
		{

			textEditorControl.Document.HighlightingStrategy = HighlightingStrategyFactory.CreateHighlightingStrategy(highlightingStrategy);
			textEditorControl.Document.TextContent = value;
			textEditorControl.ShowLineNumbers = true;

		}

		private void SetCombox()
		{

			cmbRule.Items.Add(new ComboxItem("非Null", "NotNull"));
			cmbRule.Items.Add(new ComboxItem("不能为空", "NotEmpty"));
			cmbRule.Items.Add(new ComboxItem("不等于", "NotEqual"));
			cmbRule.Items.Add(new ComboxItem("等于", "Equal"));
			cmbRule.Items.Add(new ComboxItem("长度", "Length"));
			cmbRule.Items.Add(new ComboxItem("小于", "LessThan"));
			cmbRule.Items.Add(new ComboxItem("小于或等于", "LessThanOrEqual"));
			cmbRule.Items.Add(new ComboxItem("大于", "GreaterThan"));
			cmbRule.Items.Add(new ComboxItem("大于或等于", "GreaterThanOrEqual"));
			cmbRule.Items.Add(new ComboxItem("必须", "Must"));
			cmbRule.Items.Add(new ComboxItem("匹配", "Matches"));
			cmbRule.Items.Add(new ComboxItem("错误提示", "WithMessage"));
			
			cmbRule.SelectedIndex = 0;
		}

		private void cmbRule_SelectedValueChanged(object sender, EventArgs e)
		{
			string str = cmbRule.SelectedItem.ToString();
            string val = ((ComboxItem)cmbRule.Items[cmbRule.SelectedIndex]).Value;

			switch (val)
			{
				case "Length":
                    txtCondition.Visible = true;
                    txtCondition.Text = "(0,200)";
					break;
				case "WithMessage":
                    txtCondition.Visible = true;
                    txtCondition.Text = "请填写验证失败提示";
					break;
				default:
                    txtCondition.Visible = false;
                    txtCondition.Text = "";
					break;
			}

		}

        private void btnAddCondition_Click(object sender, EventArgs e)
        {
            string condition = txtCondition.Text;
            string val = ((ComboxItem)cmbRule.Items[cmbRule.SelectedIndex]).Value;
            string rule = txtRule.Text;
            switch (val)
            {
                case "Length":
                    txtRule.Text = rule + ".Length"+condition;
                    break;
                case "WithMessage":
                    txtRule.Text = rule + ".WithMessage(" + condition+")";
                    break;
                default:
                    txtRule.Text = rule + "." + val + "()";
                    break;
            }


        }

        private void btnGenerationAll_Click(object sender, EventArgs e)
        {
            OutputWindow outform = new OutputWindow();
            outform.Show(this.DockPanel);

            outform.SetMsg("------准备生成所有层------");
             builder.ProjectInfo.Columns = tableInfo;
             outform.SetMsg("------Entity OK------");
             builder.Building(BuilderType.Entity);
             outform.SetMsg("------Mapping OK------");
             builder.Building(BuilderType.Mapping);
             outform.SetMsg("------IRepository OK------");
             builder.Building(BuilderType.IRepository);
             outform.SetMsg("------Repository OK------");
             builder.Building(BuilderType.Repository);
             outform.SetMsg("------IService OK------");
             builder.Building(BuilderType.IService);
             outform.SetMsg("------Service OK------");
             builder.Building(BuilderType.Service);
             outform.SetMsg("编译完成 -- 0 个错误，0 个警告");
             outform.SetMsg("========== 生成: 成功或最新 6 个，失败 0 个，跳过 0 个 ==========");
            
        }




	}

	public class ComboxItem
	{
		public string Text = "";

		public string Value = "";
		public ComboxItem(string _Text, string _Value)
		{
			Text = _Text;
			Value = _Value;
		}

		public override string ToString()
		{
			return Text;
		}
	}
}
